package com.xiaoye.qbproject.controller.qbzyk;

/**
 * @author : ChenYao
 * @version : 1.0
 * @description :
 * @create : 2024-12-27 2:32
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xiaoye.qbproject.controller.base.BaseController;
import com.xiaoye.qbproject.domain.AjaxResult;
import com.xiaoye.qbproject.domain.ZbInfo;
import com.xiaoye.qbproject.domain.ZbType;
import com.xiaoye.qbproject.service.ZbInfoService;
import com.xiaoye.qbproject.service.ZbTypeService;
import com.xiaoye.qbproject.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/***
 * @description: 装备类型控制器
 * @author: ChenYao
 * @ceateDate: 2024-12-27 2:41
 */

@RestController
@RequestMapping("/zbType")
public class ZbTypeController extends BaseController {
    @Autowired
    private ZbTypeService zbTypeService;
    @Autowired
    private ZbInfoService zbInfoService;

    @RequestMapping
    public AjaxResult list(ZbType zbType) {
        LambdaQueryWrapper<ZbType> queryWrapper = Wrappers.lambdaQuery();
        String zbTypeName = zbType.getName();
        if (StringUtils.isNotBlank(zbTypeName)) {
            queryWrapper.like(ZbType::getName, zbTypeName);
        }
        List<ZbType> zbTypes = zbTypeService.list(queryWrapper);
        return AjaxResult.success(zbTypes);
    }

    @GetMapping("/{id}")
    public AjaxResult getZbType(@PathVariable String id) {
        ZbType zbType = zbTypeService.getById(id);
        return AjaxResult.success(zbType);
    }

    // 查询树形结构
    @RequestMapping("/tree")
    public AjaxResult tree(@RequestParam(required = false, value = "name") String name) {
        // 查询所有装备类型
        List<ZbType> zbTypeList = zbTypeService.list(
                Wrappers.<ZbType>lambdaQuery()
                        .like(StringUtils.isNotBlank(name), ZbType::getName, name)
        );
        if (zbTypeList.isEmpty()) {
            return AjaxResult.error("暂无数据");
        } else if (zbTypeList.size() == 1) {
            return AjaxResult.success(zbTypeList);
        }
        // 组装树形结构
        List<ZbType> zbTypes = zbTypeService.buildTree(zbTypeList);
        return AjaxResult.success(zbTypes);
    }

    /**
     * @description: 查询树形结构并附带装备信息
     * @author: ChenYao
     * @return: com.xiaoye.qbproject.domain.AjaxResult
     * @ceateDate: 2024-12-28 15:21
     */
    @RequestMapping("/treeAndZbInfo")
    public AjaxResult treeAndZbInfo() {
        // 查询所有装备类型
        List<ZbType> zbTypeList = zbTypeService.list();
        if (zbTypeList.isEmpty()) {
            return AjaxResult.error("暂无数据");
        }
        for (ZbType zbType : zbTypeList) {
            zbType.setZbInfos(zbInfoService.list(Wrappers
                            .<ZbInfo>lambdaQuery()
                            .eq(ZbInfo::getType, zbType.getId())
                            .select(ZbInfo::getId, ZbInfo::getName, ZbInfo::getType)
                            .orderByAsc(ZbInfo::getCreateTime)
                    )
            );
        }
/*        ZbType notType = new ZbType();
        notType.setName("未设置类型");
        notType.setZbInfos(zbInfoService.list(Wrappers
                        .<ZbInfo>lambdaQuery().isNull(ZbInfo::getType)
                        .select(ZbInfo::getId, ZbInfo::getName, ZbInfo::getType)
                        .orderByAsc(ZbInfo::getCreateTime)
                )
        );
        zbTypeList.add(notType);*/
        // 组装树形结构
        List<ZbType> zbTypes = zbTypeService.buildTree(zbTypeList);
        return AjaxResult.success(zbTypes);
    }

    @PostMapping
    public AjaxResult addZbType(@RequestBody ZbType zbType) {
        boolean save = zbTypeService.save(zbType);
        if (save) {
            return AjaxResult.success();
        }
        return AjaxResult.error("添加失败");
    }

    @PutMapping
    public AjaxResult updateZbType(@RequestBody ZbType zbType) {
        checkZbType(zbType);
        boolean save = zbTypeService.updateById(zbType);
        if (save) {
            return AjaxResult.success();
        }
        return AjaxResult.error("更新失败");
    }

    private void checkZbType(ZbType zbType) {
        String parentId = zbType.getParentId();
        if (StringUtils.isNotBlank(parentId) && parentId.equals(zbType.getId())) {
            throw new RuntimeException("父级不能是自己");
        }
    }

    @DeleteMapping("/{id}")
    public AjaxResult deleteZbType(@PathVariable String id) {
        boolean save = zbTypeService.removeById(id);
        if (save) {
            return AjaxResult.success();
        }
        return AjaxResult.error("删除失败");
    }
}
